package kr.moiba.backoffice.user.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import kr.moiba.mail.MailSender;
import kr.moiba.user.domain.User;
import kr.moiba.user.service.UserSvc;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 이 영호(Youngho Lee)
 */

@Controller("backofficeUserCtrl")
@RequestMapping("/backoffice/user")
public class UserCtrl {
	
	private final Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private MessageSourceAccessor messages;
	
	@Autowired
	private MailSender mailSender;
	
	@Autowired
	private UserSvc userSvc;
	
	@RequestMapping(value={"/list.do"})
	public Map<String, Object> noticeList(HttpServletRequest request) throws Exception {
		
		Map<String, Object> condition = new HashMap<String, Object>();
		
		String searchType = ServletRequestUtils.getStringParameter(request, "searchType", "");
		String searchWord = ServletRequestUtils.getStringParameter(request, "searchWord", "");
		
		if(searchType.equals("id")){
			condition.put("id", searchWord);	
		}else if(searchType.equals("name")){
			condition.put("name", searchWord);
		}else if(searchType.equals("email")){
			condition.put("email", searchWord);
		}else if(searchType.equals("bizNm")){
			condition.put("bizNm", searchWord);
		}
		
		//페이징 처리 parameter
		int maxnum = ServletRequestUtils.getIntParameter(request, "maxnum", 10);
		int page = ServletRequestUtils.getIntParameter(request, "page", 1);
		condition.put("maxnum", maxnum);
		condition.put("page", (page-1)*maxnum);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.putAll(userSvc.findbyListPaging(condition));
		resultMap.put("maxnum", maxnum);
		resultMap.put("page", page);
		
		return resultMap;
	}
	
	@RequestMapping(value={"/form.do"})
	public Map<String, Object> form(HttpServletRequest request) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		Integer idx = ServletRequestUtils.getIntParameter(request, "idx");
		if(idx != null){
			resultMap.put("user", userSvc.findby(idx));
		}

		return resultMap;
	}
	
	@RequestMapping(value={"/submit.do"})
	public String submit(HttpServletRequest request, HttpSession session, User user) throws Exception {

		String userId = (String)session.getAttribute("userId");
		String userIp = request.getRemoteAddr();
		//기본 정보 지정
		user.setCreatedId(userId);
		user.setCreatedIp(userIp);
		
		if(user.getIdx() != null){//update
			String fAct = ServletRequestUtils.getStringParameter(request, "fAct", "modify");
			if(fAct.equals("remove")){//삭제
				Map<String, Object> condition = new HashMap<String, Object>();
				condition.put("modifiedId", userId);
				condition.put("modifiedIp", userIp);
				Integer[] idx = {user.getIdx()};
				condition.put("idx", idx);
				userSvc.remove(condition);
			}
			
			if(fAct.equals("reuse")){//부활
				Map<String, Object> condition = new HashMap<String, Object>();
				condition.put("modifiedId", userId);
				condition.put("modifiedIp", userIp);
				Integer[] idx = {user.getIdx()};
				condition.put("idx", idx);
				userSvc.reuse(condition);
			}
			
			if(fAct.equals("modify")){//수정
				user.setModifiedId(userId);
				user.setModifiedIp(userIp);
				userSvc.modify(user);
			}
		}else{//등록
			if(user.getAuthority().equals("ROLE_USER")){
				user.setEnabled(0);
			}else{
				user.setEnabled(1);
			}
			userSvc.add(user);
		}
		
		return "backoffice/user/result";
	}
	
	@RequestMapping(value={"/detail.do"})
	public Map<String, Object> detail(HttpSession session, @RequestParam(required=true, value="idx") Integer idx) throws Exception {
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		User user = userSvc.findby(idx);
		if(user != null){
			resultMap.put("user", user);
		}else{
			throw new Exception("[ERROR] 등록되지 않은 회원 입니다.");
		}

		return resultMap;
	}

	@RequestMapping("/chkDupl.do")
	public @ResponseBody String chkDuplication(HttpSession session, @RequestParam(required=false, value="idx")  String idx, @RequestParam(required=true, value="userId")  String id) throws Exception {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("chkDupl", true);
		condition.put("id", id);
		condition.put("idx", idx);
		return userSvc.findbyChkDupl(condition);
	}

}